//
//  BaseViewController.m
//  RootDirectory
//
//  Created by Ryan on 13-2-28.
//  Copyright (c) 2013年 Ryan. All rights reserved.
//

#import "BaseViewController.h"

@interface BaseViewController ()

@end

@implementation BaseViewController

@synthesize headerView;
@synthesize backgroundImageView;

- (NYHeaderView *)headerView
{
    if(nil == headerView)
    {
        NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:@"NYHeaderView" owner:self options:nil];
        headerView = [[nibs lastObject] retain];
        headerView.delegate = self;
    }
    return headerView;
}

- (UIImageView *)backgroundImageView
{
    if(nil == backgroundImageView)
    {
        backgroundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 44.f, self.view.frame.size.width, self.view.frame.size.height-44.f)];
        backgroundImageView.image = [UIImage imageNamed:@"bg_rightMenu@2x.png"];
    }
    return backgroundImageView;
}

#pragma mark - NYHeaderViewDelegate methods

- (void)didLeftItemTapped
{
    [self leftItemTapped];
}
- (void)didRightItemTapped
{
    [self rightItemTapped];
}

- (void)didExtraItemTapped
{
    [self extraItemTapped];
}
#pragma mark - BaseViewController methods
- (void)leftItemTapped
{}

- (void)rightItemTapped
{}

- (void)extraItemTapped
{}

- (void)setLeftNaviItemWithTitle:(NSString *)title imageName:(NSString *)imageName
{
    if(!title && !imageName)
    {
        self.headerView.leftButton.hidden = YES;
        return;
    }
    self.headerView.leftButton.hidden = NO;
    
    NSString *imgName = [NSString stringWithFormat:@"%@_normal@2x.png",imageName];
    NSString *imgHighlightName = [NSString stringWithFormat:@"%@_highlighted@2x.png",imageName];
    
    [self.headerView.leftButton setImage:[UIImage imageNamed:imgName] forState:UIControlStateNormal];
    [self.headerView.leftButton setImage:[UIImage imageNamed:imgHighlightName] forState:UIControlStateHighlighted];
}

- (void)setRightNaviItemWithTitle:(NSString *)title imageName:(NSString *)imageName
{
    if(!title && !imageName)
    {
        self.headerView.rightButton.hidden = YES;
        return;
    }
    self.headerView.rightButton.hidden = NO;
    
    NSString *imgName = [NSString stringWithFormat:@"%@_normal@2x.png",imageName];
    NSString *imgHighlightName = [NSString stringWithFormat:@"%@_highlighted@2x.png",imageName];
    
    [self.headerView.rightButton setImage:[UIImage imageNamed:imgName] forState:UIControlStateNormal];
    [self.headerView.rightButton setImage:[UIImage imageNamed:imgHighlightName] forState:UIControlStateHighlighted];
}

- (void)setNaviTitle:(NSString *)title
{
    self.trackedViewName = title;
    self.headerView.titleLabel.text = title;
}

- (void)addExtraNaviItem
{
    UIBarButtonItem *extraItem = [[UIBarButtonItem alloc] initWithTitle:@"筛选" style:UIBarButtonItemStylePlain target:self action:@selector(extraItemTapped)];
    NSArray *array = [NSArray arrayWithObjects:self.navigationItem.rightBarButtonItem,extraItem, nil];
    self.navigationItem.rightBarButtonItems = array;
    [extraItem release];
}

#pragma mark - Gesture methods
- (void)swipeWithGesture:(UISwipeGestureRecognizer *)gesture
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)addSwipePopGesture
{
    //添加页面手势划出
    UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeWithGesture:)];
    swipeGesture.direction = UISwipeGestureRecognizerDirectionRight;
    [self.view addGestureRecognizer:swipeGesture];
    [swipeGesture release];
}

#pragma mark - UIViewController methods

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    [self.view addSubview:self.backgroundImageView];
    [self.view sendSubviewToBack:self.backgroundImageView];
    [self.view addSubview:self.headerView];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    if(self.isViewLoaded && !self.view.window)
    {
        self.view = nil;
        self.headerView = nil;
        self.backgroundImageView = nil;
    }
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    self.headerView = nil;
    self.backgroundImageView = nil;
}

- (void)dealloc
{
    [backgroundImageView release];
    [headerView release];
    [super dealloc];
}

@end
